﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;

public partial class GetState : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string WebUrl;
        string ANIValue;
        string StateValue;
        string AreaCode;
        WebUrl = "";
        ANIValue = Request.QueryString["ANI"];
        AreaCode = ANIValue.Substring(0, 3);
        WebUrl = "http://www.webservicex.net/uszip.asmx/GetInfoByAreaCode?USAreaCode=" + AreaCode;
        WebRequest wrReq;
        wrReq = WebRequest.Create(WebUrl);
        WebResponse response = wrReq.GetResponse();
        Stream ReceiveStream = response.GetResponseStream();
        StreamReader readStream = new StreamReader(ReceiveStream,true);

        string str;
        int pos1, pos2;
        str = readStream.ReadToEnd();
        if (str.IndexOf("<NewDataSet />") > 0)
        {
            Literal1.Text = "";
        }
        else
        {
            pos1 = str.IndexOf("<STATE>") + 7;
            pos2 = str.IndexOf("</STATE>");
            response.Close();
            readStream.Close();
            StateValue = str.Substring(pos1, pos2 - pos1);
            Literal1.Text = StateValue;
        }
    }
}